package com.hdjk.view.activity.financial;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.activity.main.LoginActivity;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.config.Constant;
import com.hdjk.view.entity.CustomFundEntity;
import com.hdjk.view.network.CustomFundlNetWork;
import com.hdjk.view.util.StringAppUtil;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.DensityUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.Arrays;
import java.util.List;

/**
 * 预约基金详情页
 * Created by sxy on 2017/11/2.
 */

public class CustomFundDetailActivity extends BaseActivity implements View.OnClickListener {
    private String id;
    private CustomFundEntity mFund;

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private TextView tv_name;
    private TextView tv_fund_type;
    private TextView tv_fund_code;
    private TextView tv_introduce;
    private LinearLayout lin_more;
    private TextView tv_fill_name;
    private TextView tv_custodian;
    private TextView tv_min_initial_ipo_amount;
    private TextView tv_end_date;
    private TextView tv_launch_date;
    private TextView tv_investment_objective;
    private TextView tv_launch_size;
    private TextView tv_min_subseq_ipo_amount;
    private TextView tv_ipo_start_date;
    private TextView tv_more;
    private LinearLayout lin_images;
    private TextView tv_buy;
    private ScrollView sv_main1,sv_main2;
    private Button btn_sure, btn_cancel;
    private ImageView iv_bg;
    // End Of Content View Elements
    private void bindViews() {

        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_fund_type = (TextView) findViewById(R.id.tv_fund_type);
        tv_fund_code = (TextView) findViewById(R.id.tv_fund_code);
        tv_introduce = (TextView) findViewById(R.id.tv_introduce);
        lin_more = (LinearLayout) findViewById(R.id.lin_more);
        tv_fill_name = (TextView) findViewById(R.id.tv_fill_name);
        tv_custodian = (TextView) findViewById(R.id.tv_custodian);
        tv_min_initial_ipo_amount = (TextView) findViewById(R.id.tv_min_initial_ipo_amount);
        tv_end_date = (TextView) findViewById(R.id.tv_end_date);
        tv_launch_date = (TextView) findViewById(R.id.tv_launch_date);
        tv_investment_objective = (TextView) findViewById(R.id.tv_investment_objective);
        tv_launch_size = (TextView) findViewById(R.id.tv_launch_size);
        tv_min_subseq_ipo_amount = (TextView) findViewById(R.id.tv_min_subseq_ipo_amount);
        tv_ipo_start_date = (TextView) findViewById(R.id.tv_ipo_start_date);
        tv_more = (TextView) findViewById(R.id.tv_more);
        lin_images = (LinearLayout) findViewById(R.id.lin_images);
        tv_buy = (TextView) findViewById(R.id.tv_buy);
        iv_bg = (ImageView) findViewById(R.id.iv_bg);
        sv_main1 = (ScrollView) findViewById(R.id.sv_main1);
        sv_main2 = (ScrollView) findViewById(R.id.sv_main2);
        btn_sure = (Button) findViewById(R.id.btn_sure);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_custom_fund_detail);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_more.setOnClickListener(this);
        lin_more.setVisibility(View.GONE);
        Drawable drawable = getResources().getDrawable(R.drawable.more);
        drawable.setBounds(0, 0, DensityUtil.dip2px(CustomFundDetailActivity.this, 18), DensityUtil.dip2px(CustomFundDetailActivity.this, 13));
        tv_more.setCompoundDrawables(null, null, drawable, null);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        ViewGroup.LayoutParams layoutParams2 = iv_bg.getLayoutParams();
        layoutParams2.width = dm.widthPixels;
        layoutParams2.height = (int) ((float) (dm.widthPixels) / 1086 * 1932);
        iv_bg.setLayoutParams(layoutParams2);
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        getCustomFundDetail(id);
    }

    private void getCustomFundDetail(String id) {
        mDialog.show();
        CustomFundlNetWork.getCustomFundDetail(UserTokenUtil.getToken(CustomFundDetailActivity.this), id, new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                mDialog.cancel();
                if (msg != null && msg.getSuccess() && msg.getData() != null) {
                    mFund = (CustomFundEntity) msg.getData();
                    setViews();
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });
    }

    private void setViews() {
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundName()))
            tv_name.setText(mFund.getFundName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundName()))
            tv_fill_name.setText(mFund.getFundName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundCustodian()))
            tv_custodian.setText(mFund.getFundCustodian());
        if (!StringAppUtil.isEmptyOrNull(mFund.getMinInitialIpoAmount()))
            tv_min_initial_ipo_amount.setText(mFund.getMinInitialIpoAmount());
        if (!StringAppUtil.isEmptyOrNull(mFund.getEndDate()))
            tv_end_date.setText(mFund.getEndDate());
        if (!StringAppUtil.isEmptyOrNull(mFund.getLaunchDate())) {
            tv_launch_date.setText(mFund.getLaunchDate());
        }
        if (!StringAppUtil.isEmptyOrNull(mFund.getInvestmentObjective()))
            tv_investment_objective.setText(mFund.getInvestmentObjective());
        if (!StringAppUtil.isEmptyOrNull(mFund.getLaunchSize()))
            tv_launch_size.setText(mFund.getLaunchSize() + "亿元");
        if (!StringAppUtil.isEmptyOrNull(mFund.getMinSubseqIpoAmount()))
            tv_min_subseq_ipo_amount.setText(mFund.getMinSubseqIpoAmount() + "元");
        if (!StringAppUtil.isEmptyOrNull(mFund.getIpoStartDate()))
            tv_ipo_start_date.setText(mFund.getIpoStartDate());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundType()))
            tv_fund_type.setText(mFund.getFundType());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundCode()))
            tv_fund_code.setText(mFund.getFundCode());
        if (!StringAppUtil.isEmptyOrNull(mFund.getRemark()))
            tv_introduce.setText(mFund.getRemark());
        if ("1".equals(getIntent().getStringExtra("state"))) {
            tv_buy.setText("开始预约");
            tv_buy.setBackgroundResource(R.drawable.sure_button_selector);
            tv_buy.setOnClickListener(this);
        } else if ("0".equals(getIntent().getStringExtra("state"))) {
            tv_buy.setText("即将开售");
            tv_buy.setBackgroundResource(R.color.black_999999);
        } else {
            tv_buy.setText("已售罄");
            tv_buy.setBackgroundResource(R.color.black_999999);
        }
        if (!StringAppUtil.isEmptyOrNull(mFund.getImgs())) {
            DisplayImageOptions options=new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();
            String[] imgs = mFund.getImgs().replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\"", "").split(",");
            for (int i = 0; i < imgs.length; i++) {
                ImageView imageView = new ImageView(this);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                ImageLoader.getInstance().displayImage(Constant.CUSTOM_FUND_IMG_URL + imgs[i], imageView,options);
                lin_images.addView(imageView);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_more:
                if (lin_more.getVisibility() == View.VISIBLE) {
                    lin_more.setVisibility(View.GONE);
                    tv_more.setText("查看更多");
                    Drawable drawable = getResources().getDrawable(R.drawable.more);
                    drawable.setBounds(0, 0, DensityUtil.dip2px(CustomFundDetailActivity.this, 18), DensityUtil.dip2px(CustomFundDetailActivity.this, 13));
                    tv_more.setCompoundDrawables(null, null, drawable, null);
                } else {
                    lin_more.setVisibility(View.VISIBLE);
                    tv_more.setText("收起");
                    Drawable drawable = getResources().getDrawable(R.drawable.shouqi);
                    drawable.setBounds(0, 0, DensityUtil.dip2px(CustomFundDetailActivity.this, 18), DensityUtil.dip2px(CustomFundDetailActivity.this, 13));
                    tv_more.setCompoundDrawables(null, null, drawable, null);
                }
                break;
            case R.id.tv_buy:
                if (UserTokenUtil.isLogin(CustomFundDetailActivity.this)) {
                    String isFirst = UserTokenUtil.isFirst(this);
                    if (TextUtils.isEmpty(isFirst)) {
                        sv_main2.setVisibility(View.GONE);
                        sv_main1.setVisibility(View.VISIBLE);
                        btn_sure.setOnClickListener(this);
                        btn_cancel.setOnClickListener(this);
                        return;
                    } else {
                        List<String> list = Arrays.asList(isFirst.split(","));
                        if (!list.contains(UserTokenUtil.getUserName(this))) {
                            sv_main2.setVisibility(View.GONE);
                            sv_main1.setVisibility(View.VISIBLE);
                            btn_sure.setOnClickListener(this);
                            btn_cancel.setOnClickListener(this);
                            return;
                        } else {
                            sv_main2.setVisibility(View.VISIBLE);
                            sv_main1.setVisibility(View.GONE);
                        }
                    }
                    Intent intent = new Intent(CustomFundDetailActivity.this, CustomFundBuyActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("fund", mFund);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(CustomFundDetailActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.btn_sure:
                UserTokenUtil.setIsFirst(this, UserTokenUtil.isFirst(this) + UserTokenUtil.getUserName(this) + ",");
                sv_main2.setVisibility(View.VISIBLE);
                sv_main1.setVisibility(View.GONE);
                Intent intent = new Intent(CustomFundDetailActivity.this, CustomFundBuyActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("fund", mFund);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.btn_cancel:
                sv_main2.setVisibility(View.VISIBLE);
                sv_main1.setVisibility(View.GONE);
                break;
        }
    }
}
